<cfcomponent displayname="Controller" output="false" extends="ModelGlue.Core.Controller">

<!--- WARNING

The following are "reserved" terms, used by the base
ModelGlue.Core.Controller.

Do not name functions any of the following:

GetModelGlue
GetControllerName
AddToCache
ExistsInCache
GetFromCache
RemoveFromCache

Do not declare variables in the variables scope named the following:

variables.ModelGlue


		 /WARNING --->


<!--- Constructor --->
<cffunction name="Init" access="Public" returnType="Controller" output="false">
	<cfargument name="ModelGlue" required="true" type="ModelGlue.ModelGlue" />
	<cfargument name="InstanceName" required="true" type="string" />
	<cfset super.Init(arguments.ModelGlue) />

	<!--- create an instance of the PageGateway --->
	<cfset variables.pageGateway = createObject("component", "#getModelGlue().getConfigSetting("root")#.model.PageGateway").init(getModelGlue().getConfigSetting("dsn"))>

	<!--- create an instance of the PageDAO --->	
	<cfset variables.pageDAO = createObject("component", "#getModelGlue().getConfigSetting("root")#.model.PageDAO").init(getModelGlue().getConfigSetting("dsn")) />
	
	<!--- Controllers are in the application scope:  Put any application startup code here. --->
	<cfreturn this />
</cffunction>

<!--- Functions specified by <message-listener> tags --->
<cffunction name="OnRequestStart" access="Public" returntype="void" output="false" hint="I am an event handler.">
	<cfargument name="event" type="ModelGlue.Core.Event" required="true">
	<cfset var pageOb = getPageGateway().getPage(arguments.event.getValue("path"), arguments.event.getValue("version", 0))>
	<cfset var webpath = cgi.script_name>
	
	<cfset webpath = replace(webpath, "/index.cfm", "")>
	
	<!--- Every request has a page. --->
	<cfset arguments.event.setValue("page", pageOb) />
	<!--- Save current web path. This is used in layout. --->
	<cfset arguments.event.setValue("webpath", webpath)>
	<!--- put app title in view state --->
	<cfset arguments.event.setValue("appTitle", getModelGlue().getConfigSetting("root"))>
		
</cffunction>

<cffunction name="OnRequestEnd" access="Public" returntype="void" output="false" hint="I am an event handler.">
	<cfargument name="event" type="ModelGlue.Core.Event" required="true">
</cffunction>

<cffunction name="commitPage" access="Public" returnType="ModelGlue.Core.Event" output="false" hint="Handles adding/updating a page.">
	<cfargument name="event" type="ModelGlue.Core.Event" required="true">

	<cfset var page = arguments.event.getValue("page") />
	<cfset var errors = "">
	<cfset var path = "">
	<cfset var theurl = "">
	
	<cfset page.setID(createUUID()) />
	<cfset page.setPath(page.getPath()) />
	<cfset page.setBody(arguments.event.getValue("body")) />
	<cfset page.setDateTimeCreated(now()) />
	<cfset page.setAuthor(arguments.event.getValue("author")) />
	<cfif page.getVersion() neq "">
		<cfset page.setVersion(page.getVersion()+1) />
	<cfelse>
		<cfset page.setVersion(1)>
	</cfif>
	<cfset page.setSummary(arguments.event.getValue("summary")) />
	
	<cfset errors = page.validate()>	

	<cfif arrayLen(errors)>
		<cfset arguments.event.addResult("invalid") />
		<cfset arguments.event.setValue("errors", errors) />
	<cfelse>
		<cfset getPageDAO().create(page) />
		
		<!--- Notify the admin? --->
		<cfif len(getModelGlue().getConfigSetting("emailOnChange"))>
			<cfset path = arguments.event.getValue("webpath")>
			<cfset theurl = "http">
			<cfif len(cgi.https) and cgi.https is not "off">
				<cfset theurl = theurl & "s">
			</cfif>

			<cfset theurl = theurl & "://#cgi.server_name##cgi.script_name#/#page.getPath()#">
			
			<cfmail to="#getModelGlue().getConfigSetting("emailOnChange")#" from="#getModelGlue().getConfigSetting("emailOnChange")#" subject="Canvas Notification: Page Update" type="html">
			<cfoutput>
			Page Updated: <a href="#theurl#">#page.getPath()#</a><br />
			<cfif len(page.getAuthor())>
			Auhor: #page.getAuthor()#<br />
			</cfif>
			<cfif len(page.getSummary())>
			Summary: #page.getSummary()#<br />
			</cfif>
			Version: #page.getVersion()#<br />
			Updated: #dateFormat(page.getDateTimeCreated(), "short")# #timeFormat(page.getDateTimeCreated(), "short")#<br />
			<hr>
			#variables.pageGateway.render(page,path)#
			</cfoutput>
			</cfmail>
		</cfif>
		
		<cfset arguments.event.addResult("success") />
	</cfif>

	<cfreturn arguments.event />
</cffunction>

<cffunction name="editInstructions" access="public" returnType="ModelGlue.Core.Event" output="false"
			hint="Gets the documentation for editing pages.">
	<cfargument name="event" type="ModelGlue.Core.Event" required="true">
			
	<cfset arguments.event.setValue("editInstructions", getPageGateway().getRenderInstructions()) />

	<cfreturn arguments.event>
</cffunction>

<cffunction name="getPageChildren" access="public" returnType="ModelGlue.Core.Event" output="false"
			hint="Gets all of the page names of pages that are children to this page.">
	<cfargument name="event" type="ModelGlue.Core.Event" required="true">
				
	<cfset arguments.event.setValue("pagechildren", getPageGateway().getPageChildren(arguments.event.getValue("page"))) />
	
	<cfreturn arguments.event>
</cffunction>

<cffunction name="getPageDAO" access="private" output="false">
	<cfreturn variables.pageDAO>
</cffunction>

<cffunction name="getPageGateway" access="private" output="false">
	<cfreturn variables.pageGateway>
</cffunction>

<cffunction name="getPageRender" access="public" returnType="ModelGlue.Core.Event" output="false"
			hint="Gets the render for the current page.">
	<cfargument name="event" type="ModelGlue.Core.Event" required="true">
				
	<cfset arguments.event.setValue("pagerender", getPageGateway().render(arguments.event.getValue("page"),arguments.event.getValue("webpath"))) />
	
	<cfreturn arguments.event>
</cffunction>

<cffunction name="getPageHistory" access="public" returnType="ModelGlue.Core.Event" output="false"
			hint="Gets the history for the current page.">
	<cfargument name="event" type="ModelGlue.Core.Event" required="true">
				
	<cfset arguments.event.setValue("pagehistory", getPageGateway().getPageHistory(arguments.event.getValue("page"))) />
	
	<cfreturn arguments.event>
</cffunction>

<cffunction name="getPageIndex" access="public" returnType="ModelGlue.Core.Event" output="false"
			hint="Gets all of the page names.">
	<cfargument name="event" type="ModelGlue.Core.Event" required="true">
				
	<cfset arguments.event.setValue("pageindex", getPageGateway().getPageIndex()) />
	
	<cfreturn arguments.event>
</cffunction>

<cffunction name="search" access="public" returnType="ModelGlue.Core.Event" output="false"
			hint="Search Wiki.">
	<cfargument name="event" type="ModelGlue.Core.Event" required="true">
	<cfset var searchTerms = arguments.event.getValue("searchterms")>
	
	<cfif searchTerms neq "">
		<cfset arguments.event.setValue("searchresults", getPageGateway().search(searchterms)) />
	</cfif>
	
	<cfreturn arguments.event>
</cffunction>

</cfcomponent>

